﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
using System.Text;
namespace Web
{
    public partial class SubPolicy : System.Web.UI.Page
    {
        protected  int mainId = 0;
        private int pageId = 1;
        protected int SubjectId = 0;
        protected int  subId=0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterQuesType"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            GetUrlParams();
            if (!IsPostBack)
            {
                if (mainId != 0 && pageId != 0)
                {
                    LoadSubPolicy();
                }
                if (subId > 0)
                {
                    BindingQuestionType();
                    ShowSubInfo();
                }
            }

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (mainId != 0 && pageId != 0)
            {
                LoadSubPolicy();
            }
        }
        void ShowSubInfo()
        {
             TestPaperPolicySubInfo sub =null;
             try
             {
                 sub = TestPaperPolicySub.GetT1(subId);
             }
             catch
             {
                 sub = null;
             }
             if (sub == null)
             {
                 return;
             }
            this.tbName.Value = sub.Name;
            this.tbOrder.Value = sub.Order.ToString();
            string text = "";
            string value = "";
            if (sub.Content != null && sub.Content != "")
            {
                XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                #region anthor
                if (xmlQuestion.StartDate != null && xmlQuestion.EndDate != null && xmlQuestion.AppearNum != null)
                {

                    this.tbRepeater.Text = String.Format("从{0}至{1}已抽取{2}次的题目不选用",
                                                        xmlQuestion.StartDate.Value.ToString("yyyy-MM-dd HH:mm"),
                                                        xmlQuestion.EndDate.Value.ToString("yyyy-MM-dd HH:mm"),xmlQuestion.AppearNum);
                }


                if (xmlQuestion.Type != null && xmlQuestion.Type != "")
                {
                    this.drpQuestionType.SelectedValue = xmlQuestion.Type;
                }
                if (xmlQuestion.AllCount != 0)
                {
                    this.tbAllCount.Text = xmlQuestion.AllCount.ToString();
                }
                else
                {
                    this.tbAllCount.Text = "";
                }
                if (xmlQuestion.AllScore != 0)
                {
                    this.tbAllScore.Text = xmlQuestion.AllScore.ToString();
                }
                else
                {
                    this.tbAllScore.Text = "";
                }
                if (xmlQuestion.SingleScore != null && xmlQuestion.SingleScore.Count() > 0)
                {
                    this.tbSingleScore.Text = xmlQuestion.SingleCondtion;
                }
                else
                {
                    this.tbSingleScore.Text = "";
                }

                #endregion
                #region diff
               


                if (xmlQuestion.Diffs != null && xmlQuestion.Diffs.Count() > 0)
                {
                    lbDiff.Items.Clear();
                    XmlDiff diff = null;
                    for (int i = 0; i < xmlQuestion.Diffs.Count(); i++)
                    {
                        diff = xmlQuestion.Diffs[i];
                        value = diff.DiffId;
                        DifficInfo diffInfo = Diffic.GetDifficInfoById(int.Parse(value));
                        if (diffInfo != null && diffInfo.Id != 0)
                        {
                            text = "难度" + diff.DiffOptionType + diffInfo.Keyword + "占" + string.Format("{0:0%}", diff.DiffTypePercent);
                            lbDiff.Items.Add(new ListItem(text, value));
                        }
                    }
                }
                else
                {
                    lbDiff.Items.Clear();
                }

                #endregion
                #region know
                if (xmlQuestion.KnowPoints != null && xmlQuestion.KnowPoints.Count() > 0)
                {
                    this.lbKownPoint.Items.Clear();
                    XmlKnowPoint point = null;
                    for (int i = 0; i < xmlQuestion.KnowPoints.Count(); i++)
                    {
                        point = xmlQuestion.KnowPoints[i];
                        value = point.KnowPointId;
                        if (value != "")
                        {
                            KnowledgeInfo pointInfo = Knowledge.GetKnowledgeInfoById(int.Parse(value));
                            if (pointInfo != null && pointInfo.Id != 0)
                            {
                                text =pointInfo.Keyword+"  "+ pointInfo.Name + "占" + string.Format("{0:0%}", point.KnowPointTypePercent);
                                lbKownPoint.Items.Add(new ListItem(text, value));
                            }
                        }
                    }

                }
                else
                {
                    this.lbKownPoint.Items.Clear();
                }

                #endregion

            }
        }
        void BindingQuestionType()
        {
            IList<QuesTypeInfo> list = QuesType.GetQuesTypeInfos(0, int.MaxValue);
            this.drpQuestionType.DataTextField = "Name";
            this.drpQuestionType.DataValueField = "Id";
            this.drpQuestionType.DataSource = list;
            this.drpQuestionType.DataBind();
        }
        private void GetUrlParams()
        {
            if (this.Request["id"] != null && this.Request["id"].ToString() != "")
            {
                mainId = int.Parse(this.Request["id"].ToString());
            }
            if (this.Request["page"] != null && this.Request["page"].ToString() != "")
            {
                pageId = int.Parse(this.Request["page"].ToString());
            }
            if (this.Request["subId"] != null && this.Request["subId"].ToString() != "")
            {
                subId = int.Parse(this.Request["subId"].ToString());
            }
            if (this.Request["SubjectId"] != null && this.Request["SubjectId"].ToString() != "")
            {
                SubjectId = int.Parse(this.Request["SubjectId"].ToString());
            }
        }
        protected void LoadSubPolicy()
        {
            IList<TestPaperPolicySubInfo> subPolicys = TestPaperPolicySub.GetList(mainId);
            StringBuilder sb = new StringBuilder();
            sb.Append("<ul class='list2'>");
            foreach (var  info in subPolicys)
            {
                sb.Append("<li>");
                sb.Append("<span><a id=\"sub"+info.Id.ToString()+"\" target=\"_self\" href=\"SubPolicy.aspx?subId=");
                sb.Append(info.Id);
                sb.Append("&SubjectId=" + SubjectId.ToString());
                sb.Append("&page=" + pageId.ToString() + "&id=" + mainId);
                sb.Append("\">" + info.Name + "</a> ");
                sb.Append("[<a href=\"javascript:ShowConfirm('SubPolicyUpdate.aspx?delid=");
                sb.Append(info.Id + "&mainId=" + mainId + "&page=" + pageId.ToString() + "&subId=" + subId.ToString());
                sb.Append("&SubjectId=" + SubjectId.ToString());
                sb.Append("','确定要删除？',150,60)\">删除</a>]</span>");
                sb.Append("</li>");
                
            }
            sb.Append("</ul>");
            this.lbQuestionType.Text = sb.ToString();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("PolicyMain.aspx?page="+pageId.ToString());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (subId != 0)
            {
                TestPaperPolicySubInfo sub = null;

                try
                {
                    sub = TestPaperPolicySub.GetT1(subId);
                }
                catch
                {
                    sub = null;
                }
                if (sub == null)
                {
                    return;
                }
                #region
                sub.Name = this.tbName.Value;
                sub.Order = int.Parse(this.tbOrder.Value);
                XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(sub.Content);
                xmlQuestion.AllCount = int.Parse(this.tbAllCount.Text);
                xmlQuestion.AllScore = int.Parse(this.tbAllScore.Text);
                xmlQuestion.Type = this.drpQuestionType.SelectedValue;
                string singleScore = this.tbSingleScore.Text;
                if (singleScore != null && singleScore != "")
                {
                    string[] singleScores = singleScore.Split(' ');
                    xmlQuestion.SingleScore = new List<decimal>();
                    foreach (string item in singleScores)
                    {
                        xmlQuestion.SingleScore.Add(decimal.Parse(item));
                    }
                }

                #endregion
                #region save
                try
                {
                    sub.Content = XmlQuestionHelper.GetContent(xmlQuestion);
                    TestPaperPolicySub.Edit(sub);
                    if (sub != null && sub.Id != 0)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "OnSucceeded('ok');", true);
                        LoadSubPolicy();
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "OnSucceeded('failure');", true);
                    }
                }
                catch
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "OnSucceeded('failure');", true);
                }
                #endregion

            }
        }
    }
}

